package com.snake.mylibrary.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.snake.mylibrary.utils.LogUtil;

public class MirrorLayout extends FrameLayout {
	private static final int MIRROR_HEIGHT = 80;// 镜像的默认高度，单位是dp
	private static final int MIRROR_MARGIN = 3;// 镜像与子控件的高度
	private int mMirrorHeight;
	private int mMirrorMargin;
	private Bitmap mMirrorBitmap;
	private Paint mPaint;
	private Canvas mCanvas;
	private boolean mShowMirror = true;

	public MirrorLayout(Context context) {
		super(context);
		InitView();
	}

	public MirrorLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		InitView();
	}

	public MirrorLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		InitView();
	}

	private void InitView() {
		/*mMirrorHeight = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, MIRROR_HEIGHT, getResources().getDisplayMetrics());
		mMirrorMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, MIRROR_MARGIN, getResources().getDisplayMetrics());*/
//		mMirrorHeight = (int)getResources().getDimension(R.dimen.mirrorlayout_height);
//		mMirrorMargin = (int)getResources().getDimension(R.dimen.mirrorlayout_margin);
		mMirrorHeight=200;
		mMirrorMargin=100;
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
	}

	public void setShowMirror(boolean v)
	{
		mShowMirror = v;
		requestLayout();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (getChildCount() <= 0) {
			return;
		}
		if(!mShowMirror)
		{
			return ;
		}

		int child_height = getChildAt(0).getMeasuredHeight();
		LogUtil.e("child_height"+child_height);
		child_height += mMirrorHeight + mMirrorMargin;
		LogUtil.e("child_height"+child_height);
		setMeasuredDimension(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
				MeasureSpec.makeMeasureSpec(child_height, MeasureSpec.EXACTLY));//设置mirror的测量规则
		if (mMirrorBitmap != null && !mMirrorBitmap.isRecycled()) {
			if (mMirrorBitmap.getHeight() != child_height) {
				mMirrorBitmap.recycle();
				mMirrorBitmap = null;
			}
		}
		if (mMirrorBitmap == null) {
			mMirrorBitmap = Bitmap.createBitmap(getMeasuredWidth(), mMirrorHeight,
					Config.ARGB_8888);
			mCanvas = new Canvas(mMirrorBitmap);//根据bitmap创建的画布
		}
	}

	public void setMirrorHeight(int height) {
		if (height < 30) {
			throw new IllegalArgumentException("mirror height is to small");
		}
		mMirrorHeight = height;
		requestLayout();
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		try {
			super.dispatchDraw(canvas);
			if(!mShowMirror)
			{
				return ;
			}
			if (getChildCount() == 0) {
				return;
			}
			if(mMirrorBitmap == null )
			{
				return ;
			}
			drawMirrorBitmap(canvas);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void drawMirrorBitmap(Canvas canvas)
	{
		View child = getChildAt(0);
		LogUtil.e("left:"+child.getLeft()+"top:"+child.getTop()+"right:"+child.getRight()+"bottom:"+child.getBottom());
		mCanvas.save();
		Matrix m = new Matrix();
		m.preTranslate(0f, child.getMeasuredHeight()*1.0f);
		m.preScale(1f, -1f);
		mCanvas.setMatrix(m);
		child.draw(mCanvas);
		mCanvas.restore();
		
		LinearGradient linear = new LinearGradient(0, 0, 0, mMirrorBitmap.getHeight(), 0xA2ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
		Paint paint = new Paint();
		paint.setShader(linear);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
		mCanvas.drawRect(0, 0, mMirrorBitmap.getWidth(), mMirrorBitmap.getHeight(), paint);
		
		
		canvas.drawBitmap(mMirrorBitmap, child.getX(), child.getMeasuredHeight()
				+ mMirrorMargin, mPaint);
	}

	@Override
	public void addView(View child) {
		if (getChildCount() > 0) {
			throw new IllegalStateException(
					"MirrorLayout can host only one direct child");
		}

		super.addView(child);
	}

	@Override
	public void addView(View child, int index) {
		if (getChildCount() > 0) {
			throw new IllegalStateException(
					"MirrorLayout can host only one direct child");
		}

		super.addView(child, index);
	}

	@Override
	public void addView(View child, ViewGroup.LayoutParams params) {
		if (getChildCount() > 0) {
			throw new IllegalStateException(
					"MirrorLayout can host only one direct child");
		}

		super.addView(child, params);
	}

	@Override
	public void addView(View child, int index, ViewGroup.LayoutParams params) {
		if (getChildCount() > 0) {
			throw new IllegalStateException(
					"MirrorLayout can host only one direct child");
		}

		super.addView(child, index, params);
	}
	
	@Override
	protected void onDetachedFromWindow() {
		if(getChildCount() != 0){
			removeAllViews();
		}
		mCanvas = null;
		mPaint = null;
		super.onDetachedFromWindow();
		if (mMirrorBitmap != null && !mMirrorBitmap.isRecycled()) {
			mMirrorBitmap.recycle();
			mMirrorBitmap = null;
		}
	}

}
